package com.hsu.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hsu.hiring.company.dao.PositionDao;
import com.hsu.hiring.company.entity.PositionEntity;

/**
 * Position Servlet
 *
 * @author Hsu.
 * @since 2016年1月14日
 */
public class PositionServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private PositionDao positionDao = new PositionDao();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("list".equals(command)) {
			list(request, response);
		} else if ("get".equals(command)) {
			get(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 所有招聘职位的列举
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、调用前台传来的参数
		// 2、调用业务逻辑
		List<PositionEntity> positionList = positionDao.list();
		// 3、转向
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("view/company/position-list.jsp").forward(
				request, response);
	}

	/**
	 * 根据职位id查询所对应详细信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void get(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、调用前台传来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		PositionEntity position = positionDao.get(id);
		// 3、转向
		request.setAttribute("position", position);
		request.getRequestDispatcher("view/company/position-detail.jsp")
				.forward(request, response);
	}
}
